﻿using System;
using System.Collections.Generic;
using DBAccessLibrary.Model;
using System.Collections.ObjectModel;
using System.Linq;

namespace DBAccessLibrary
{
    /// <summary>
    /// 系统缓存
    /// </summary>
    public static class SysCache
    {
     
        static ConnectionModel _connection;
        /// <summary>
        /// 当前数据库链接
        /// </summary>
        public static ConnectionModel DBConnection 
        {
            get 
            {
                if(_connection == null)
                        throw new Exception("请先连接一个数据库再进行此操作");
                return _connection;
            }
            set { _connection = value; }
        }
        /// <summary>
        /// 当前数据库的所有表信息
        /// </summary>
        public static IEnumerable<TableEntity> TableList
        {
            get; set;
        }
     
        /// <summary>
        /// 存储过程信息
        /// </summary>
        public static IEnumerable<ProcEntity> ProcList
        {
            get;
            set;
        }
       
        /// <summary>
        /// 视图信息
        /// </summary>
        public static IEnumerable<ViewEntity> ViewList
        {
            get;
            set;
        }

        /// <summary>
        /// 类型信息
        /// </summary>
        public static IEnumerable<TypeInfoEntity> TypeList { get; set; }

        /// <summary>
        /// 记录当前连接过的所有数据库
        /// </summary>
        public static ObservableCollection<DataBaseEntity> ServerList { get; set; }
    }

    public class DbEntityCache
    {
        //当前数据库的所有表信息
        public IEnumerable<TableEntity> TableList
        {
            get;
            set;
        }

        //存储过程信息
        public IEnumerable<ProcEntity> ProcList
        {
            get;
            set;
        }

        //视图信息
        public IEnumerable<ViewEntity> ViewList
        {
            get;
            set;
        }
        /// <summary>
        /// 总个数（表+视图+存储过程）
        /// </summary>
        public int Count
        {
            get
            {
                int count = 0;
                if (TableList != null)
                    count += TableList.Count();
                if (ViewList != null)
                    count += ViewList.Count();
                if (ProcList != null)
                    count += ProcList.Count();
                return count;
            }
        }
    }
}
